package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.album.service.impl.VodServiceImpl;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Objects;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	@GuiGuLogin
	public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(
			@PathVariable Long albumId,@PathVariable Long page,@PathVariable Long limit
	){
		Page<AlbumTrackListVo> pageL = new Page<>(page,limit);
		Page<AlbumTrackListVo> list = this.trackInfoService.findAlbumTrackPage(pageL,albumId);

		return Result.ok(list);
	}

	@Operation(summary = "上传声音")
	@PostMapping("uploadTrack")
	public Result<Map<String, Object>> uploadTrack(MultipartFile file){
		Map<String, Object> map = 	vodService.uploadTrack(file);
		return Result.ok(map);
	}

	@Operation(summary = "新增音频")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		this.trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	@Operation(summary = "根据用户ID和条件分页查询所有音频信息")
	@PostMapping("findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Long page, @PathVariable Long limit, @RequestBody TrackInfoQuery trackInfoQuery){

		Page<TrackListVo> page1 = new Page<>(page,limit);
		Page<TrackListVo> list = this.trackInfoService.findUserTrackPage(page1,trackInfoQuery);


		return	Result.ok(list);
	}


	//api/album/trackInfo/getTrackInfo/51942
	@Operation(summary = "根据trackid回显")
	@GetMapping("getTrackInfo/{trackId}")
	public Result<TrackInfo> getTrackInfo(@PathVariable("trackId") Long trackId){
		TrackInfo trackInfo = this.trackInfoService.getById(trackId);

		return Result.ok(trackInfo);
	}

	@Operation(summary = "根据trackId修改")
	@PutMapping("updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId")Long trackId,@RequestBody TrackInfoVo trackInfoVo){

		this.trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}

	@Operation(summary = "根据trackId删除音频")
	@DeleteMapping("removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable Long trackId){
		this.trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	@GetMapping("getTrackStatVo/{trackId}")
	@GuiGuLogin
	@Operation(summary = "获取播放进度")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		TrackStatVo trackStatVo = this.trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}



}

